<template>
	<div class="dinv" style="margin-top: 16px">
		<el-button type="primary" size="mini" class="btns1" @click="addTr" :disabled="isBaseDisabled" v-show="!['4', '6', '2'].includes(dataType)">
			添加 <i class="el-icon-circle-plus-outline"></i
		></el-button>
		<div>
			<span v-if="['6'].includes(dataType)">
				<span style="color: red; font-weight: bold" v-show="fundCompare()">(未复核)</span>
			</span>
		</div>
		<el-table :data="fundManagerList" height="570" border style="width: 100%">
			<el-table-column label="序号" type="index" width="50">
				<template slot-scope="scope"
					>{{ scope.$index + 1 }}
					<span v-if="type == '4'" style="color: red">（未复核）</span>
				</template>
			</el-table-column>
			<el-table-column label="姓名" width="200">
				<template slot-scope="scope">
					<el-select
						v-model="scope.row.managerId"
						filterable
						clearable
						@change="changeManagerInfo(scope.$index)"
						:disabled="isBaseDisabled || !!scope.row.fundManagerId || !!scope.row.fundManagerApplyId"
					>
						<el-option v-for="(item, index) in managerList" :key="index + 'a'" :label="item.managerName" :value="item.managerId">
						</el-option>
					</el-select>
				</template>
			</el-table-column>
			<el-table-column label="证件类型">
				<template slot-scope="scope">
					<el-select v-model="scope.row.idType" filterable clearable disabled>
						<el-option key="0101" label="身份证" value="0101"></el-option>
						<el-option key="0102" label="护照" value="0102"></el-option>
						<el-option key="0103" label="港澳居民来往内地通行证" value="0103"></el-option>
						<el-option key="0104" label="台湾居民来往大陆通行证" value="0104"></el-option>
						<el-option key="0105" label="港澳台居民居住证" value="0105"></el-option>
						<el-option key="0106" label="军官证" value="0106"></el-option>
						<el-option key="0107" label="士兵证" value="0107"></el-option>
						<el-option key="0108" label="户口本" value="0108"></el-option>
						<el-option key="0109" label="文职证" value="0109"></el-option>
						<el-option key="0110" label="警官证" value="0110"></el-option>
						<el-option key="0111" label="社会保障号" value="0111"></el-option>
						<el-option key="0112" label="外国护照" value="0112"></el-option>
						<el-option key="0113" label="外国人永久居留证" value="0113"></el-option>
						<el-option key="0199" label="其他人员证件" value="0199"></el-option>
					</el-select>
				</template>
			</el-table-column>
			<el-table-column label="证件号码">
				<template slot-scope="scope">
					<el-input v-model="scope.row.idNo" type="text" disabled></el-input>
				</template>
			</el-table-column>
			<el-table-column label="任职日期">
				<template slot-scope="scope">
					<!-- :disabled="isBaseDisabled || !!scope.row.fundManagerId || !!scope.row.fundManagerApplyId" -->
					<el-date-picker
						type="date"
						v-model="scope.row.startDate"
						value-format="yyyyMMdd"
						format="yyyy-MM-dd"
						:disabled="isBaseDisabled"
					></el-date-picker>
				</template>
			</el-table-column>
			<el-table-column label="离职日期">
				<template slot-scope="scope">
					<el-date-picker
						type="date"
						v-model="scope.row.endDate"
						value-format="yyyyMMdd"
						format="yyyy-MM-dd"
						:disabled="isBaseDisabled"
					></el-date-picker>
				</template>
			</el-table-column>
			<el-table-column label="任职公告日期">
				<template slot-scope="scope">
					<!-- :disabled="isBaseDisabled || !!scope.row.fundManagerId || !!scope.row.fundManagerApplyId" -->
					<el-date-picker
						type="date"
						v-model="scope.row.entryNoticeDate"
						value-format="yyyyMMdd"
						format="yyyy-MM-dd"
						:disabled="isBaseDisabled"
					></el-date-picker>
				</template>
			</el-table-column>
			<el-table-column label="离职公告日期">
				<template slot-scope="scope">
					<el-date-picker
						type="date"
						v-model="scope.row.leaveNoticeDate"
						value-format="yyyyMMdd"
						format="yyyy-MM-dd"
						:disabled="isBaseDisabled"
					></el-date-picker>
				</template>
			</el-table-column>
			<el-table-column prop="operation" label="操作" v-if="!isBaseDisabled" width="130">
				<template slot-scope="scope">
					<el-button v-if="!scope.row.fundManagerId && !scope.row.fundManagerApplyId" size="mini" type="primary" @click="addTr()"
						><i class="el-icon-plus"></i
					></el-button>
					<el-button
						class="delebtn"
						v-if="!scope.row.fundManagerId && !scope.row.fundManagerApplyId"
						size="mini"
						type="danger"
						@click="removeTr(scope.$index)"
						><i class="el-icon-minus"></i
					></el-button>
				</template>
			</el-table-column>
		</el-table>
	</div>
</template>
<script>
import { fundSAll } from '@/api/productApi/fundManager'
import { saveManager, seachStep } from '@/api/productApi/infoMain'
import { fundCompare } from '@/views/infoMain/publicList/components/dataCheck'
import { jsonArray } from '@/api/productApi/file'
export default {
	props: {
		setpFunc: {
			type: Function,
			default: Function
		},
		fundSysCode: {
			type: String,
			default: ''
		},
		dataType: {
			type: String,
			default: ''
		}
	},
	data() {
		return {
			fundManagerList: [], // 基金-基金经理显示信息
			type: '', // 类型：1新增 、2查看、3编辑、4新增复核、5重新申请（新增驳回）、6编辑复核
			isBaseDisabled: false,
			managerList: [], // 基金经理列表下拉框
			fundCompare
		}
	},
	mounted() {
		this.selectManagerList()
	},
	methods: {
		// 重新申请将数据集中在store
		updateStoreData() {
			this.$store.state.fundStep.pucliktTotalData.fundManagerApplyList = this.fundManagerList.filter(item => !!item.managerId)
		},

		// 当单点保存时，使用最新数据响应
		getNewfundMange() {
			const params = {
				fundRaiseType: '1',
				fundSysCode: this.fundSysCode,
				filedCheckStatus: this.dataType === '5' || this.dataType === '7' ? '2' : '0'
			}
			seachStep(params).then(res => {
				if (res.msgCode === 'Y') {
					this.edit(res.data.body.fundManagerApplyList)
				}
			})
		},

		// 完善信息切换步骤时保存切换前页面数据
		async switchingSteps() {
			const params = {}
			params.fundManagerApplyList = this.fundManagerList.filter(item => !!item.managerId)
			params.fundRaiseType = '1'
			params.fundSysCode = this.fundSysCode
			await saveManager(params).then(res => {
				if (res.msgCode === 'Y') {
					this.$emit('setpFunc', '1', this.fundSysCode, '', 1)
				}
			})
			return true
		},

		// 新增:【保存】和【下一步】
		startDown(type) {
			this.isCheck = true
			this.CustomtextComfirm(() => {
				const params = {}
				params.fundManagerApplyList = this.fundManagerList.filter(item => !!item.managerId)
				params.fundRaiseType = '1'
				params.fundSysCode = this.fundSysCode
				saveManager(params).then(res => {
					if (res.msgCode === 'Y') {
						if (type === '下一步') {
							this.$emit('setpFunc', '1', this.fundSysCode, '', 0)
						} else {
							this.$emit('setpFunc', '1', this.fundSysCode, '', 1)
						}
						this.$message({ type: 'success', message: res.msg })
					}
				})
			}, `此操作将永久提交已编辑数据，是否继续？`)
		},

		isBaseDisabledCom(data, msgShow) {
			this.isBaseDisabled = data
			this.isCheck = msgShow
		},

		async edit(data) {
			await this.selectManagerList()
			const list = data || []
			this.$nextTick(() => {
				list.forEach(item => {
					if (this.managerList.some(items => items.managerId === item.managerId)) {
						const obj = this.managerList.filter(items => items.managerId === item.managerId)
						this.$set(item, 'idType', obj[0].idType)
						this.$set(item, 'idNo', obj[0].idNo)
						this.$set(item, 'managerName', obj[0].managerName)
						this.$set(item, 'managerId', obj[0].managerId)
					}
				})
				this.fundManagerList = jsonArray(list)
				this.$forceUpdate(this.fundManagerList)
			})
		},

		// 基金经理删除
		removeTr(index) {
			this.operatorComfirm(() => {
				this.fundManagerList.splice(index, 1)
			}, '删除该条数据')
		},

		// 基金经理增加
		addTr() {
			const item = { managerId: '', fundSysCode: this.fundSysCode, filedCheckStatus: '0', fundType: '1', manageStatus: '0' }
			this.fundManagerList.push(item)
		},

		// 基金经理列表（下拉框）
		async selectManagerList() {
			await fundSAll({ managerTypeList: ['1'] }).then(res => {
				if (res.msgCode === 'Y') {
					this.managerList = res.data.body
				}
			})
		},

		// 改变基金经理下拉框时
		changeManagerInfo(index) {
			this.managerList.forEach(item => {
				if (item.managerId === this.fundManagerList[index].managerId) {
					// 基金经理姓名
					this.$set(this.fundManagerList[index], 'managerName', item.managerName)
					// 身份证类型
					this.$set(this.fundManagerList[index], 'idType', item.idType)
					// 身份证号
					this.$set(this.fundManagerList[index], 'idNo', item.idNo)
					// 任职时间
					this.$set(this.fundManagerList[index], 'startDate', '')
					// 离职时间
					this.$set(this.fundManagerList[index], 'endDate', '')
					// 任职公告时间
					this.$set(this.fundManagerList[index], 'entryNoticeDate', '')
					// 离职公告时间
					this.$set(this.fundManagerList[index], 'leaveNoticeDate', '')
				}
			})
		}
	}
}
</script>
<style lang="scss" scoped>
.dinv {
	position: relative;
	.btns1 {
		margin: 0px 0 10px 0;
	}
}
.delebtn {
	background-color: #f56c6c;
	&:hover,
	&:active {
		background-color: #f56c6c;
	}
}
</style>
